package org.origin.ferret.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.origin.centre.model.base.XPage;
import org.origin.centre.model.dto.XDeleteByIdDto;
import org.origin.centre.model.dto.XQueryByIdDto;
import org.origin.centre.model.dto.XUpdateByIdDto;
import org.origin.centre.model.enums.result.RS;
import org.origin.centre.model.result.R;
import org.origin.centre.model.vo.XPageVo;
import org.origin.ferret.system.model.dto.user.SysUserInsertDto;
import org.origin.ferret.system.model.dto.user.SysUserPageDto;
import org.origin.ferret.system.model.dto.user.SysUserPasswordDto;
import org.origin.ferret.system.model.dto.user.SysUserUpdateDto;
import org.origin.ferret.system.model.vo.user.SysUserVo;
import org.origin.ferret.system.service.SysUserService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户-接口
 *
 * @author ferret
 * @version 2024-05-08
 */
@RestController
@Tag(name = "系统用户-接口")
@RequiredArgsConstructor
@RequestMapping("${origin.api:}/sys/user")
@SuppressWarnings({"ELValidationInJSP", "SpringElInspection"})
public class SysUserController {
    private final SysUserService userService;

    @PostMapping("page")
    @Operation(summary = "1.分页数据", description = "分页数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:user:page')")
    public R<XPageVo<SysUserVo>> page(@Valid @RequestBody XPage<SysUserPageDto> pageDto) {
        return R.ok(RS.OK, this.userService.page(pageDto));
    }

    @GetMapping("query")
    @Operation(summary = "2.查询数据", description = "查询数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:user:query')")
    public R<SysUserVo> query(@Valid @ParameterObject XQueryByIdDto queryDto) {
        return R.ok(RS.OK, this.userService.query(queryDto));
    }

    @PutMapping("insert")
    @Operation(summary = "3.插入数据", description = "插入数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:user:insert')")
    public R<Boolean> insert(@Valid @RequestBody SysUserInsertDto insertDto) {
        return R.ok(RS.OK, this.userService.insert(insertDto));
    }

    @PostMapping("update")
    @Operation(summary = "4.更新数据", description = "更新数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:user:update')")
    public R<Boolean> update(@Valid @RequestBody SysUserUpdateDto updateDto) {
        return R.ok(RS.OK, this.userService.update(updateDto));
    }

    @DeleteMapping("delete")
    @Operation(summary = "5.删除数据", description = "删除数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:user:delete')")
    public R<Boolean> delete(@Valid @ParameterObject XDeleteByIdDto deleteDto) {
        return R.ok(RS.OK, this.userService.delete(deleteDto));
    }

    @PostMapping("valid")
    @Operation(summary = "6.生效数据", description = "生效数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:user:valid')")
    public R<Boolean> valid(@Valid @RequestBody XUpdateByIdDto updateDto) {
        return R.ok(RS.OK, this.userService.valid(updateDto));
    }

    @PostMapping("invalid")
    @Operation(summary = "7.失效数据", description = "失效数据")
    @PreAuthorize("principal.hasAnyAuthority('sys:user:invalid')")
    public R<Boolean> invalid(@Valid @RequestBody XUpdateByIdDto updateDto) {
        return R.ok(RS.OK, this.userService.invalid(updateDto));
    }

    @PostMapping("password")
    @Operation(summary = "8.修改密码", description = "修改密码")
    @PreAuthorize("principal.hasAnyAuthority('sys:user:password')")
    public R<Boolean> password(@Valid @RequestBody SysUserPasswordDto passwordDto) {
        return R.ok(RS.OK, this.userService.password(passwordDto));
    }

}
